//
//  TLPlayerImageView.m
//  PiyingkeLibrary
//
//  Created by Tianlong on 2017/3/24.
//
//

#import "TLCoverView.h"
#import "TLAVPlayerPublicFile.h"

@interface TLCoverView ()
/** 响应事件的按钮 */
@property (nonatomic , weak) UIButton *actionBtn;

@end

@implementation TLCoverView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self setupOtherUI];
        [self setupReplayView];
        
        self.userInteractionEnabled = YES;
        
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if(self = [super initWithCoder:aDecoder]){
        
    }
    return self;
}

#pragma mark - 设置其他UI
-(void)setupOtherUI{
    /** 封面图 */
    UIImageView *coverV = [[UIImageView alloc] init];
    _coverV = coverV;
    _coverV.contentMode = UIViewContentModeScaleToFill;
    _coverV.clipsToBounds = YES;
    _coverV.image = [UIImage imageNamed:@"videoCover.jpg"];
    _lastImage = _coverV.image;
    [self addSubview:_coverV];
    
    /** 播放icon */
    UIImageView *playIconV = [[UIImageView alloc] init];
    _playIconV = playIconV;
    _playIconV.contentMode = UIViewContentModeScaleAspectFill;
    _playIconV.clipsToBounds = YES;
    _playIconV.image = [UIImage imageNamed:@"cover_play"];
    //_playIconV.image = [UIImage imageWithBundleName:@"cover_play"];
    [self addSubview:_playIconV];
    
    
    UIButton *actionBtn = [[UIButton alloc] init];
    self.actionBtn = actionBtn;
    [self.actionBtn addTarget:self action:@selector(tapCover) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.actionBtn];
    
    
    WS(ws);
    [self.coverV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.mas_top);
        make.left.equalTo(ws.mas_left);
        make.right.equalTo(ws.mas_right);
        make.bottom.equalTo(ws.mas_bottom);
    }];
    
    [self.playIconV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(ws.mas_centerX);
        make.centerY.equalTo(ws.mas_centerY);
        make.height.offset(60);
        make.width.offset(60);
    }];
    
    [self.actionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.mas_top);
        make.left.equalTo(ws.mas_left);
        make.right.equalTo(ws.mas_right);
        make.bottom.equalTo(ws.mas_bottom);
    }];
}

#pragma mark - 添加重播视图
-(void)setupReplayView{
    //重播重播视图
    UIView *rePlayV = [[UIView alloc] init];
    self.rePlayV = rePlayV;
    self.rePlayV.hidden = YES;
    [self addSubview:self.rePlayV];
    
    WS(ws);
    [rePlayV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.mas_top);
        make.left.equalTo(ws.mas_left);
        make.right.equalTo(ws.mas_right);
        make.bottom.equalTo(ws.mas_bottom);
    }];
    
    UIView *rePlayVBG = [[UIView alloc] init];
    rePlayVBG.backgroundColor = [UIColor blackColor];
    rePlayVBG.alpha = 0.5;
    [self.rePlayV addSubview:rePlayVBG];
    UITapGestureRecognizer *tapCoverBGV = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCoverBGV:)];
    [rePlayVBG addGestureRecognizer:tapCoverBGV];
    
    [rePlayVBG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(rePlayV.mas_top);
        make.left.equalTo(rePlayV.mas_left);
        make.right.equalTo(rePlayV.mas_right);
        make.bottom.equalTo(rePlayV.mas_bottom);
    }];
    
    CGFloat iconW = 47;
    UIImageView *replayIcon = [[UIImageView alloc] init];
    //replayIcon.image = [UIImage imageWithBundleName:@"tv_bt_load"];
    replayIcon.userInteractionEnabled = YES;
    replayIcon.backgroundColor = [UIColor redColor];
    [self.rePlayV addSubview:replayIcon];
    
    UITapGestureRecognizer *tapReplay = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapReplay:)];
    [replayIcon addGestureRecognizer:tapReplay];
    
    [replayIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(rePlayV.mas_centerY);
        make.centerX.equalTo(rePlayV.mas_centerX).offset(-iconW*0.5-20);
        make.width.offset(47);
        make.height.offset(47);
    }];
    
    UIImageView *replayShareIcon = [[UIImageView alloc] init];
    //replayShareIcon.image = [UIImage imageWithBundleName:@"tv_bt_share"];
    replayShareIcon.backgroundColor = [UIColor blueColor];
    replayShareIcon.userInteractionEnabled = YES;
    [self.rePlayV addSubview:replayShareIcon];
    
    UITapGestureRecognizer *tapShare = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapShare:)];
    [replayShareIcon addGestureRecognizer:tapShare];
    
    [replayShareIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(rePlayV.mas_centerY);
        make.centerX.equalTo(rePlayV.mas_centerX).offset(iconW*0.5+20);
        make.width.offset(47);
        make.height.offset(47);
    }];
}

-(void)tapCoverBGV:(UITapGestureRecognizer *)tap{NSLog(@"这个方法是为了显示重播页面时不响应播放器的tap事件");}

#pragma mark - 点击重播
-(void)tapReplay:(UITapGestureRecognizer *)tap{
    if (self.replayBlock) {
        self.replayBlock();
    }
}

#pragma mark - 点击分享
-(void)tapShare:(UITapGestureRecognizer *)tap{
    if (self.shareBlock) {
        self.shareBlock();
    }
}

#pragma mark - 点击了封面图
-(void)tapCover{
    if (self.tapCoverView) {
        self.tapCoverView();
    }
}



-(void)layoutSubviews{
    [super layoutSubviews];
}

@end
